﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using HouseKeepingPL.HKService;
using System.Windows.Data;
using System.Windows.Documents;
using GalaSoft.MvvmLight.Messaging;

namespace HouseKeepingPL.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        #region privates
        private User _currentUser = null; // current user logged
        #endregion

        public MainViewModel()
        {
            //IsLoggedIn = false;
            DataTypeBulk _dtb = ClientProxy.Proxy.GetDataTypes(); // all data types initialize on startup of the viewmodel, one call
            Messenger.Default.Send<ObservableCollection<UserType>, AccountViewModel>(_dtb.UserTypesData); // send user type data to account control
            Messenger.Default.Send<ObservableCollection<ActionType>, IncomesViewModel>(_dtb.ActionIncomeTypesData); // send message  get income types data 
            Messenger.Default.Send<ObservableCollection<ActionType>, OutcomesViewModel>(_dtb.ActionOutcomeTypesData); // send message  get outcome types data
            Messenger.Default.Send<ObservableCollection<ReportType>, ReportViewModel>(_dtb.ReportTypesData); // send message  to report viewmodel for it's types
            Messenger.Default.Register<User>(this, GetUser);
            Messenger.Default.Register<AccountMsgs>(this, GetAccountMsgs); // for log out option


            #region RelayCommands

            // common relay commands
            ApplicationExitCommand = new RelayCommand(() => ApplicationExitCommandExcecute(), () => true);
            AboutCommand = new RelayCommand(() => AboutCommandExcecute(), () => true);
            LogoutCommand = new RelayCommand(() => LogoutCommandExcecute(), () => IsLoggedIn);

            #endregion
        }

        #region messaging actions
        private void GetUser(User user)
        {
            _currentUser = user;
        }

        private void GetAccountMsgs(AccountMsgs msg)
        {
            if (msg == AccountMsgs.UserConnected)
            {
                StatusBarConnectedAs = _currentUser.Username;
                //TabIndexProperty = (int)Tabs.Outcome; // put focus for outcome tab
                TabIndexProperty = (int)Tabs.Income; // put focus for income tab
                IsLoggedIn = _currentUser == null ? false : true;
            }

            if (msg == AccountMsgs.UserDisconnected)
            {
                StatusBarConnectedAs = null;
                IsLoggedIn = _currentUser == null ? false : true;
            }

        }
        #endregion

        #region common


        private bool _isLoggedIn = false;

        public bool IsLoggedIn
        {
            get { return _isLoggedIn; }
            set
            {
                _isLoggedIn = value;
                RaisePropertyChanged("IsLoggedIn");
            }

        }

        /// <summary>
        /// invoking killing of the app
        /// </summary>
        public ICommand ApplicationExitCommand { get; private set; }
        private void ApplicationExitCommandExcecute()
        {
            if (MyMessageBox.Confirm("Are you sure you want to exit?", "Exit Application") == MessageBoxResult.Yes)
            {
                TabIndexProperty = (int)Tabs.Account;
                Messenger.Default.Send<AccountMsgs, AccountViewModel>(AccountMsgs.UserDisconnected); // clean user data before disconnecting
                Application.Current.Shutdown();

            }

        }

        /// <summary>
        /// shows the about msg box
        /// </summary>
        public ICommand AboutCommand { get; private set; }
        private void AboutCommandExcecute()
        {
            string aboutString = String.Format("House Keeping by Roi Katz, 2012");
            MyMessageBox.InfoOk(aboutString, "About House Keeping");
        }

        private int _TabIndexProperty;
        public int TabIndexProperty
        {
            get { return _TabIndexProperty; }
            set
            {
                _TabIndexProperty = value;
                if (_TabIndexProperty == (int)Tabs.Report) // report tab
                {
                    Messenger.Default.Send<ReportMsgs, ReportViewModel>(ReportMsgs.RefreshReports);
                }

                RaisePropertyChanged("TabIndexProperty");
            }
        }

        public ICommand LogoutCommand { get; private set; }
        private void LogoutCommandExcecute()
        {
            TabIndexProperty = (int)Tabs.Account;
            Messenger.Default.Send<AccountMsgs, AccountViewModel>(AccountMsgs.UserDisconnected);
        }
                
        
        #endregion

        #region statusBar
        private string _statusBarConnectedAs;
        public string StatusBarConnectedAs
        {
            get
            {
                if (_currentUser != null)
                {
                    _statusBarConnectedAs = string.Format("Connected as: {0}", _currentUser.Username);
                    RaisePropertyChanged("StatusBarLogonTime");
                    return _statusBarConnectedAs;
                }
                else
                {
                    RaisePropertyChanged("StatusBarLogonTime");
                    return "Disconnected";
                }
            }
            set
            {
                _statusBarConnectedAs = value;
                RaisePropertyChanged("StatusBarConnectedAs");
            }
        }

        private string _statusBarLogonTime;
        public string StatusBarLogonTime
        {
            get
            {
                if (_currentUser != null)
                {
                    _statusBarLogonTime = string.Format("Logon time: {0}", DateTime.Now);
                    return _statusBarLogonTime;
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                _statusBarLogonTime = value;
                RaisePropertyChanged("StatusBarLogonTime");
            }
        }
        #endregion

        
    }
}

